package gs.sr.tkfx.srv;

import gs.sr.tkfx.param.AppInfoBase;
import gs.sr.tkfx.param.AppInfoOfTkfx;
import gs.sr.tkfx.param.Req;
import gs.sr.tkfx.taoke.Commodity;
import gs.sr.tkfx.util.TaobaoClientFactory;

import java.io.IOException;

import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import org.apache.commons.lang.StringUtils;
import org.apache.commons.lang.builder.ToStringBuilder;

import com.taobao.api.TaobaoClient;
import com.taobao.api.domain.TaobaokeItem;
import com.taobao.api.domain.TaobaokeItemDetail;
import com.taobao.api.response.TaobaokeItemsConvertResponse;
import com.taobao.api.response.TaobaokeItemsDetailGetResponse;

@SuppressWarnings("serial")
public class TkfxseServlet extends HttpServlet {
	private static final AppInfoBase appInfoBase = AppInfoOfTkfx.getInstance();
	private static final TaobaoClient client = TaobaoClientFactory
			.getTaobaoClient(appInfoBase);

	public void doGet(HttpServletRequest req, HttpServletResponse resp)
			throws IOException {
		StringBuilder sb = new StringBuilder();
		String nick = req.getParameter("nick");
		if (StringUtils.isBlank(nick)) {
			throw new IllegalArgumentException("nick cannot be empty!");
		} else {
			if (nick.length() > 50) {
				throw new IllegalArgumentException("nick overflow!");
			}
		}
		String[] iids = req.getParameterValues("iid");
		if (iids == null || iids.length < 1 || iids.length > 10) {
			throw new IllegalArgumentException("invalid arg of iid!");
		}

		Req tmpreq = new Req();
		tmpreq.setNick(nick);
		for (String iid : iids) {
			long iidlong = Long.parseLong(iid);
			if (iidlong < 1) {
				throw new IllegalArgumentException("invalid arg of iid(" + iid
						+ ")!");
			}
			tmpreq.addItemId(iidlong);
		}

		sb.append("\r\n\r\nReq:{\r\n").append(tmpreq.toString())
				.append("\r\n}");
		// convert
		try {
			TaobaokeItemsConvertResponse response = Commodity.convert(client,
					tmpreq);
			sb.append("\r\n\r\n[taobao.taobaoke.items.convert]Resp:{\r\n")
					.append(ToStringBuilder.reflectionToString(response))
					.append("\r\n}");
			if (response.isSuccess()) {
				sb.append("\r\n\r\nDetail:{\r\n");
				for (TaobaokeItem d : response.getTaobaokeItems()) {
					if (false) {
						sb.append(ToStringBuilder.reflectionToString(d));
					} else {
						sb.append("clickUrl: ").append(d.getClickUrl())
								.append("\r\n").append("shopClickUrl: ")
								.append(d.getShopClickUrl()).append("\r\n")
								.append("sellerCreditScore: ")
								.append(d.getSellerCreditScore())
								.append("\r\n").append("item.id: ")
								.append(d.getNumIid()).append("\r\n")
								.append("item.title: ").append(d.getTitle())
								.append("\r\n").append("commission: ")
								.append(d.getCommission()).append("\r\n")
								.append("commissionRate: ")
								.append(d.getCommissionRate()).append("\r\n")
								.append("picUrl: ").append(d.getPicUrl())
								.append("\r\n");
					}
					sb.append("\r\n");
				}
				sb.append("\r\n}");
			}
		} catch (Exception e) {
			resp.getWriter().print(e);
			return;
		}
		sb.append("\r\n\r\n============================================================================");
		// get
		try {
			TaobaokeItemsDetailGetResponse response = Commodity.get(client,
					tmpreq);
			sb.append("\r\n\r\n[taobao.taobaoke.items.detail.get]Resp:{\r\n")
					.append(ToStringBuilder.reflectionToString(response))
					.append("\r\n}");
			if (response.isSuccess()) {
				sb.append("\r\n\r\nDetail:{\r\n");
				for (TaobaokeItemDetail d : response.getTaobaokeItemDetails()) {
					sb.append("clickUrl: ").append(d.getClickUrl())
							.append("\r\n").append("shopClickUrl: ")
							.append(d.getShopClickUrl()).append("\r\n")
							.append("sellerCreditScore: ")
							.append(d.getSellerCreditScore()).append("\r\n")
							.append("item.id: ")
							.append(d.getItem().getNumIid()).append("\r\n")
							.append("item.title: ")
							.append(d.getItem().getTitle()).append("\r\n");
					sb.append("\r\n");
				}
				sb.append("\r\n}");
			}
		} catch (Exception e) {
			resp.getWriter().print(e);
			return;
		}

		resp.setContentType("text/plain");
		resp.setCharacterEncoding("UTF-8");
		resp.getWriter().println(sb.toString());
	}
}
